//
//  FoodViewController.m
//  HealthA段
//
//  Created by dllo on 15/6/3.
//  Copyright (c) 2015年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "FoodViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIImageView+WebCache.h"
#import "FoodCell.h"
#import "Food.h"
#import "FoodDatilViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface FoodViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UITableView *foodTable;
@property(nonatomic,retain)NSMutableArray *foodArr;
@end

@implementation FoodViewController
- (void)dealloc
{
    [_foodArr release];
    [super dealloc];
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.foodArr = [NSMutableArray array];
    [self PleaseData];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    [self creatView];
    
    
}
//创建视图
-(void)creatView
{
    self.foodTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 - 49) style:UITableViewStylePlain];
    self.foodTable.rowHeight = 120;
    self.foodTable.delegate =self;
    self.foodTable.dataSource = self;
    self.foodTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_foodTable];
    [_foodTable release];
}

#pragma mark 协议方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.foodArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    FoodCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (cell == nil) {
        cell = [[[FoodCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse]autorelease];
    }
    Food *f = self.foodArr[indexPath.row];
    NSString *str = @"http://www.yi18.net/";
    NSString *pivUrl = [str stringByAppendingString:f.img];
    NSURL *url = [NSURL URLWithString:pivUrl];
    [cell.picView sd_setImageWithURL:url];
    cell.nameLabel.text = f.name;
    cell.foodLabel.text = f.food;
    return cell;
}

//数据请求
-(void)PleaseData
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager  GET:@"http://api.yi18.net/cook/list?page=1&limit=20" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *arr = [dic objectForKey:@"yi18"];
        for (NSMutableDictionary *tep in arr) {
            Food *f = [[Food alloc]init];
            [f setValuesForKeysWithDictionary:tep];
            [self.foodArr addObject:f];
            [f release];
        }
        [self.foodTable reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败请求");
    }];
}

//跳转详情页面
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FoodDatilViewController *fvc = [[FoodDatilViewController alloc]
                                init];
    Food *f = self.foodArr[indexPath.row];
    fvc.title = f.name;
    fvc.id = f.id;
    NSLog(@"id ===%ld",f.id);
    [self.navigationController pushViewController:fvc animated:YES];
    [fvc release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
